<template>
  <!-- 模态框宽度 -->
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="560px"
    @open="handleDialogOpen()"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>

    <!-- 添加模态框 -->
    <entryteam-edit
      ref="entryteamEdit"
      v-on:save-finished="TeamhandleClick()"
    ></entryteam-edit>
    <role-permission ref="rolePermission"></role-permission>

    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="first">
        <el-form
          :model="SetmealModel"
          :rules="formDietary"
          ref="SetMealForm"
          label-width="110px"
          label-position="right"
          size="small"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="套餐编码" prop="code">
                <el-input v-model="SetmealModel.code" ref="code"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="套餐名称" prop="name">
                <el-input v-model="SetmealModel.name" ref="name"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="套餐助记" prop="helpcode">
                <el-input
                  v-model="SetmealModel.helpcode"
                  ref="helpcode"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="适用性别" prop="sex">
                <el-select v-model="SetmealModel.sex">
                  <el-option
                    ref="sex"
                    v-for="item in sex"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="年龄下限" prop="setmealUp">
                <el-input
                  type="number"
                  max="100"
                  min="1"
                  v-model="SetmealModel.setmealUp"
                  ref="setmealUp"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="年龄上限" prop="setmealDown">
                <el-input
                  type="number"
                  max="100"
                  min="1"
                  v-model="SetmealModel.setmealDown"
                  ref="setmealDown"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="套餐价格" prop="price">
                <el-input v-model="SetmealModel.price" ref="price"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="成本价格" prop="costPrice">
                <el-input
                  v-model="SetmealModel.costPrice"
                  ref="costPrice"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="图片" prop="img" style="height:40px;">
                <el-upload
                  :on-change="getName"
                  :http-request="zdy"
                  action="aaa"
                >
                  <el-input v-model="img" placeholder="请上传图片"></el-input>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="套餐介绍" prop="remark">
            <el-input
              type="textarea"
              resize="none"
              v-model="SetmealModel.remark"
            ></el-input>
          </el-form-item>

          <el-form-item label="注意事项" prop="attention">
            <el-input
              type="textarea"
              resize="none"
              v-model="SetmealModel.attention"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-tab-pane>

      <!-- 项目信息 -->
      <el-tab-pane label="项目信息" name="two">
        <el-form
          style="height:300px;"
          :model="SetmealModel"
          label-width="110px"
          label-position="right"
          size="small"
        >
          <el-row>
            <el-col :span="9">
              <el-select
                size="small"
                @change="team"
                v-model="testSele"
                placeholder="项目"
              >
                <el-option
                  v-for="item in entryt"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>

            <el-col :span="11">
              <el-input
                style="width:200px;"
                size="small"
                class="input-with-select"
                v-model="etname"
              >
                <!-- 项目查询 -->
                <el-button
                  size="small"
                  slot="append"
                  icon="el-icon-search"
                  @click="handleClick"
                  v-if="clickSele"
                ></el-button>

                <!-- 项目组查询 -->
                <el-button
                  size="small"
                  slot="append"
                  icon="el-icon-search"
                  @click="TeamhandleClick"
                  v-if="!clickSele"
                ></el-button>
              </el-input>
            </el-col>

            <el-col :span="4">
              <el-button
                type="primary"
                size="small"
                @click="handleCreateSport()"
                v-if="!judgeAdd"
                >添加</el-button
              >
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-table
                :data="teamList"
                border
                size="mini"
                max-height="200"
                header-cell-class-name="header-row"
                v-if="tablesd"
              >
                <!-- 判断项目编码 -->
                <el-table-column
                  prop="coding"
                  label="项目编码"
                  header-align="center"
                ></el-table-column>

                <!-- 判断项目名称 -->
                <el-table-column
                  prop="name"
                  label="项目名称"
                  header-align="center"
                ></el-table-column>

                <!-- 判断项目介绍 -->
                <el-table-column
                  prop="expository"
                  label="项目介绍"
                  header-align="center"
                ></el-table-column>
              </el-table>

              <el-table
                :data="teamList"
                border
                size="mini"
                max-height="200"
                header-cell-class-name="header-row"
                v-if="!tablesd"
              >
                <!-- 判断项目组编码 -->
                <el-table-column
                  prop="tCoding"
                  label="项目组编码"
                  header-align="center"
                ></el-table-column>

                <!-- 判断项目组名称 -->
                <el-table-column
                  prop="tName"
                  label="项目组名称"
                  header-align="center"
                ></el-table-column>

                <!-- 判断项目介绍 -->
                <el-table-column
                  prop="tExpository"
                  label="项目组介绍"
                  header-align="center"
                ></el-table-column>

                <el-table-column
                  label="删除"
                  header-align="center"
                  v-if="!judge"
                >
                  <template slot-scope="scope">
                    <el-button
                      v-show="true"
                      type="text"
                      size="mini"
                      @click="deleteEntryTeam(scope.$index, scope.row)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submitForm('SetMealForm')"
        >保 存</el-button
      >
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addSetmeal, updateSetMeal } from "@/api/Appointment/tsetmeal";
import {
  deleteTeam,
  selectEntrys,
  selectMuchEntry,
  selectMuchEntryTeam
} from "@/api/Appointment/entryteam";
import EntryteamEdit from "@/components/Appointment/entryteam/entryTeamEdit";
import RolePermission from "@/components/role/rolePermission";

export default {
  name: "role-edit",
  data() {
    return {
      judgeAdd: true,
      judge: true,
      tablesd: true,
      clickSele: true,
      etname: "",
      activeName: "first",
      img: "",
      testSele: "",
      teamList: [],

      sex: [
        { value: "0", label: "不限" },
        { value: "1", label: "男" },
        { value: "2", label: "女" }
      ],

      entryt: [
        { value: "项目", label: "项目" },
        { value: "项目组", label: "项目组" }
      ],

      SetmealModel: [
        {
          id: "",
          name: "",
          helpcode: "",
          sex: "",
          setmealUp: "",
          setmealDown: "",
          age: "",
          price: "",
          costPrice: "",
          remark: "",
          attention: "",
          img: ""
        }
      ],

      //加载
      locked: false,

      dialogProps: {
        visible: false,
        action: "",
        title: ""
      },
      formDietary: {
        code: [{ required: true, message: "请输入套餐编码", trigger: "blur" }],
        name: [{ required: true, message: "请输入套餐名称", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],

        setmealUp: [
          { required: true, message: "请输入年龄下限", trigger: "blur" }
        ],
        setmealDown: [
          { required: true, message: "请输入年龄上限", trigger: "blur" }
        ],
        price: [{ required: true, message: "请输入套餐价格", trigger: "blur" }],
        costPrice: [
          { required: true, message: "请输入成本价格", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    team(event) {
      if (event == "项目") {
        this.judgeAdd = true;
        this.judge = true;
        this.etname = "";
        this.clickSele = true;
        this.tablesd = true;
        this.teamList = [];
        selectMuchEntry(this.etname).then(response => {
          this.teamList = response.data;
        });
      } else {
        this.judgeAdd = false;
        this.judge = false;
        this.etname = "";
        this.clickSele = false;
        this.tablesd = false;
        this.teamList = [];
        selectMuchEntryTeam(this.etname).then(response => {
          this.teamList = response.data;
        });
      }
    },

    //文件上传
    getName(file) {
      this.img = file.name;
    },

    //项目组条件查询
    TeamhandleClick() {
      selectMuchEntryTeam(this.etname)
        .then(response => {
          this.teamList = response.data;
        })
        .catch(error => {
          console.log(error);
          this.loading = false;
        });
    },

    zdy() {
      URL: "D:/S3学期/HTML5/版本控制及Axureprpro/qianduan/src/assets/mealimg";
    },

    //项目条件查询或选项卡点击查询
    handleClick() {
      if (this.testSele == "" || this.testSele == "项目") {
        selectMuchEntry(this.etname)
          .then(response => {
            this.teamList = response.data;
          })
          .catch(error => {
            console.log(error);
            this.loading = false;
          });
      } else {
        selectMuchEntryTeam(this.etname).then(response => {
          this.teamList = response.data;
        });
      }
    },

    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs["SetMealForm"].clearValidate();
        this.$refs["code"].focus();
      });
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveSetmeal();
          } else {
            this.doUpdateSetmeal();
          }
        } else {
          return false;
        }
      });
    },

    //删除项目组
    deleteEntryTeam(index, row) {
      this.$confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.loading = true;
          deleteTeam(row.eid)
            .then(response => {
              this.loading = false;
              if (response.data > 0) {
                this.TeamhandleClick();
                this.$message({
                  message: "删除成功",
                  type: "success"
                });
              }
            })
            .catch(error => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {});
    },

    doUpdateSetmeal() {
      updateSetMeal(this.SetmealModel)
        .then(response => {
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "修改成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("修改失败");
        });
    },
    doSaveSetmeal() {
      this.SetmealModel.img = this.img;
      addSetmeal(this.SetmealModel)
        .then(response => {
          this.locked = false;
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "添加成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("添加失败");
        });
    },

    handleDialogClose() {
      this.dialogProps.visible = false;
    },

    handleCreateSport() {
      this.$refs.entryteamEdit.$emit("openAddDietaryDialog");
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error"
      });
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on("openEditDietaryDialog", function(eModel) {
        this.img = eModel.img;
        this.SetmealModel = eModel;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddDietaryDialog", function() {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.SetmealModel = {};
        this.img = "";
        this.dialogProps.visible = true;
      });
    });
  },
  components: { EntryteamEdit, RolePermission }
};
</script>

<style>
.el-upload-list__item-name {
  display: none;
}
</style>